/*!
 * @file DelaySensor.cpp
 * @author Rocco Martino
 */
/***************************************************************************
 *   Copyright (C) 2014 by Rocco Martino                                   *
 *   martinorocco@gmail.com                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Lesser General Public License as        *
 *   published by the Free Software Foundation; either version 2.1 of the  *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Lesser General Public      *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

/* ======================================================================= */
/* ....................................................................... */
#include <ooGame/DelaySensor>

#include <osgODE/Notify>
#include <osgODE/RigidBody>
#include <osgODE/World>
/* ....................................................................... */
/* ======================================================================= */




using namespace ooGame ;




/* ======================================================================= */
/* ....................................................................... */
DelaySensor::DelaySensor(void):
    m_delay         ( 0 ),
    m_duration      ( 0 ),
    m_repeat        ( false ),
    m_accum         ( 0 )
{
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
DelaySensor::DelaySensor(const DelaySensor& other, const osg::CopyOp& copyop):
    Sensor          ( other, copyop ),
    m_delay         ( other.m_delay ),
    m_duration      ( other.m_duration ),
    m_repeat        ( other.m_repeat ),
    m_accum         ( other.m_accum )
{
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
DelaySensor::~DelaySensor(void)
{
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
bool
DelaySensor::pulse( osgODE::ODEObject* object, Game* game )
{
    (void) object ;
    (void) game ;


    const unsigned int  duration = osg::maximum(m_duration, (unsigned int) 1 ) ;


    if( m_repeat ) {
        m_accum = m_accum % (m_delay + duration) ;
    }




    const bool  k1 = m_accum >= m_delay ;
    const bool  k2 = (m_accum - m_delay) < duration ;
    const bool  k3 = m_duration == 0 ;


    ++m_accum ;

    return k1 && (k2 || k3) ;
}
/* ....................................................................... */
/* ======================================================================= */
